import os
import time

import pyautogui

from cmd_type import CmdType
from my_log import my_log
import script
from ui_util import parse


# 任务
def start_task(sheet):
    i = 1
    while i < sheet.nrows:
        _row = sheet.row(i)
        # 取本行的数据
        _data1 = _row[0]
        _data2 = _row[1]
        _data3 = _row[2]
        _cmd_type = _data1
        _type = int(_cmd_type.value)
        # 单击(图片)
        if _type == CmdType.CLICK_IMAGE:
            # 取图片名称
            _target_image = _data2.value
            _retry_count = 1
            if _data3.ctype == 2 and _data3.value != 0:
                _retry_count = _data3.value
            mouse_click("left", 1, _target_image, _retry_count)
            my_log.info(f"[cmd] 单击(图片): {_target_image}")
        # 双击(图片)
        elif _type == CmdType.DOUBLE_CLICK_IMAGE:
            # 取图片名称
            _target_image = _data2.value
            # 取重试次数
            _retry_count = 1
            if _data3.ctype == 2 and _data3.value != 0:
                _retry_count = _data3.value
            mouse_click("left", 2, _target_image, _retry_count)
            my_log.info(f"[cmd] 双击(图片): {_target_image}")
        # 右键(图片)
        elif _type == CmdType.RIGHT_CLICK_IMAGE:
            # 取图片名称
            _target_image = _data2.value
            # 取重试次数
            _retry_count = 1
            if _data3.ctype == 2 and _data3.value != 0:
                _retry_count = _data3.value
            mouse_click("right", 1, _target_image, _retry_count)
            my_log.info(f"[cmd] 右键(图片): {_target_image}")
        # 单击(坐标)
        elif _type == CmdType.CLICK_POINT:
            _click_point(_row)
        # 双击(坐标)
        elif _type == CmdType.DOUBLE_CLICK_POINT:
            _double_click_point(_row)
        # 右键(坐标)
        elif _type == CmdType.RIGHT_CLICK_POINT:
            _right_click_point(_row)
        # 单击(文字)
        elif _type == CmdType.CLICK_TEXT:
            _click_text(_row)
        # 双击(文字)
        elif _type == CmdType.DOUBLE_CLICK_TEXT:
            _double_click_text(_row)
        # 右键(文字)
        elif _type == CmdType.RIGHT_CLICK_TEXT:
            _right_click_text(_row)
        # 输入
        elif _type == CmdType.INPUT_TEXT:
            _input(_row)
        # 键盘按键
        elif _type == CmdType.PRESS:
            _press(_row)
        # 等待
        elif _type == CmdType.WAIT:
            _wait(_row)
        # 滚轮
        elif _type == CmdType.SCROLL:
            _scroll(_row)
        # 移动到坐标
        elif _type == CmdType.MOVE:
            _move(_row)
        # 脚本
        elif _type == CmdType.SCRIPT:
            _script(_row)
        # HotKey
        elif _type == CmdType.HOT_KEY:
            _hotkey(_row)
        # 拖动(相对)
        elif _type == CmdType.DRAG:
            _drag(_row)
        # 拖动(绝对)
        elif _type == CmdType.DRAG_TO:
            _drag_to(_row)
        i += 1

# 单击(坐标)
def _click_point(row):
    # 取本行的数据
    _data2_type = row[1].ctype
    _text = ""
    if _data2_type != 0:
        _point = row[1].value
        parts = _point.split('|')
        # 移动到位置, x表示移动过程两秒
        pyautogui.moveTo(int(parts[0]), int(parts[1]), 0.2)
        _text = f"移动->{_point}"
    pyautogui.mouseDown(button="left")
    pyautogui.mouseUp()
    my_log.info(f"[cmd] 单击(坐标): {_text} 点击")

# 双击(坐标)
def _double_click_point(row):
   pass

# 右键(坐标)
def _right_click_point(row):
    # 取本行的数据
    _data2 = row[1].value
    parts = _data2.split('|')
    # 鼠标定位到我的电脑，进行右击
    # pyautogui.mouseDown(button="right", x=int(parts[0]), y=int(parts[1]))
    # pyautogui.mouseUp()
    pyautogui.click(int(parts[0]), int(parts[1]), duration=0.2, button="right")
    my_log.info(f"[cmd] 右键(坐标): {_data2}")

# 单击(文字)
def _click_text(row):
    pass

# 双击(文字)
def _double_click_text(row):
    pass

# 右键(文字)
def _right_click_text(row):
    pass

# 输入
def _input(row):
    # 取本行的数据
    _data2 = row[1]
    _input_text = _data2.value
    parse(_input_text)
    my_log.info(f"[cmd] 输入: {_input_text}")

# 键盘按键
def _press(row):
    # 取本行的数据
    _data2 = row[1].value
    pyautogui.press(_data2)
    my_log.info(f"[cmd] 键盘按键: {_data2}")

# 等待
def _wait(row):
    # 取本行的数据
    _data2 = row[1]
    # 取时长
    _wait_time = _data2.value
    my_log.info(f"[cmd] 等待: {_wait_time}秒")
    time.sleep(_wait_time)

# 移动到坐标
def _move(row):
    # 取本行的数据
    _data2 = row[1].value
    parts = _data2.split('|')

    # 移动到位置, x表示移动过程两秒
    pyautogui.moveTo(int(parts[0]), int(parts[1]), 0.2)
    my_log.info(f"[cmd] 移动到坐标: {_data2}")


# 滚轮
def _scroll(row):
    # 取本行的数据
    _data2 = row[1]
    _data3 = row[2]
    # 取滑动距离
    scroll = _data2.value
    # 取重试次数
    _retry_count = 1
    if _data3.ctype == 2 and _data3.value != 0:
        _retry_count = _data3.value

    j = 0
    while j < _retry_count:
        pyautogui.scroll(int(scroll))
        my_log.info(f"[cmd] 滚轮: 滑动[{j + 1}] {int(scroll)}距离")
        time.sleep(0.1)
        j += 1

# 运行脚本
def _script(row):
    _file_name = row[1].value
    my_log.info(f"[cmd] 运行脚本: {_file_name}")
    script.run_script(_file_name)

# HotKey
def _hotkey(row):
    # 取本行的数据
    _data2 = row[1].value
    parts = _data2.split('+')
    pyautogui.hotkey(*parts)
    my_log.info(f"[cmd] HotKey: {_data2}")

# 拖动(相对)
def _drag(row):
    # 取本行的数据
    _data2 = row[1].value
    parts = _data2.split('|')
    # 按住左键，从当前位置向右移动100px，向下移动200px，用时2s
    pyautogui.drag(int(parts[0]), int(parts[1]), 0.5, button='left')
    my_log.info(f"[cmd] 拖动(相对): {_data2}")

# 拖动(绝对)
def _drag_to(row):
    # 取本行的数据
    _data2 = row[1].value
    parts = _data2.split('|')
    # 按住左键，用2s时间移动到100，200点的位置
    pyautogui.dragTo(int(parts[0]), int(parts[1]),  0.5, button='left')
    my_log.info(f"[cmd] 拖动(绝对): {_data2}")

def mouse_click(lOrR, clickTimes, target_image, reTry):
    # 检查图片是否存在
    # 获取当前工作目录
    current_dir = os.getcwd()
    # 拼接成绝对路径
    absolute_path = os.path.join(current_dir, target_image)

    # 检查图片是否存在
    if not os.path.exists(absolute_path):
        my_log.error(f"图片 {absolute_path} 不存在。")
        exit()

    if reTry == 1:
        while True:
            location=pyautogui.locateCenterOnScreen(target_image, confidence=0.9)
            if location is not None:
                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
                break
            my_log.info("未找到匹配图片,0.1秒后重试")
            time.sleep(0.1)
    elif reTry == -1:
        while True:
            location=pyautogui.locateCenterOnScreen(target_image, confidence=0.9)
            if location is not None:
                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
            time.sleep(0.1)
    elif reTry > 1:
        i = 1
        while i < reTry + 1:
            location=pyautogui.locateCenterOnScreen(target_image, confidence=0.9)
            if location is not None:
                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
                my_log.info("重复")
                i += 1
            time.sleep(0.1)

